package com.ljc.controller;

import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dao.UserDao;
import com.ljc.dto.Result;
import com.ljc.entity.College;
import com.ljc.entity.User;
import com.ljc.service.CollegeService;
import com.ljc.service.UserService;
import com.ljc.util.JWTUtil;
import com.ljc.vo.CollegeSearch;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("college")
@CrossOrigin
public class CollegeController {
    @Autowired
    private CollegeService collegeService;

    @RequestMapping("getColleges")
    public Result getCollege(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        log.info("get College (deanId={}):",id);
        List<College> college = collegeService.getCollege(Integer.valueOf(id));
        result.setStatus(1).setMsg("查询成功").setData(college);
        return result;
    }
    @RequestMapping("searchCollege")
    public Result searchCollege(HttpServletRequest request, @RequestBody CollegeSearch collegeSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        collegeSearch.setDeanId(Integer.valueOf(id));
        log.info("search College (deanId={}):",id);

        if (collegeSearch.getPageSize()!=-1){
            PageHelper.startPage(collegeSearch.getPageNum(),collegeSearch.getPageSize());
            List<College> colleges = collegeService.searchCollege(collegeSearch);
            PageInfo<College> collegePageInfo=new PageInfo<>(colleges);
            System.out.println(colleges.size());
            result.setStatus(1).setMsg("查询成功").setData(collegePageInfo);
        }else{
            List<College> colleges = collegeService.searchCollege(collegeSearch);
            result.setStatus(1).setMsg("查询成功").setData(colleges);
        }
        return result;
    }
    @RequestMapping("addCollege")
    public Result addColleges(HttpServletRequest request,@RequestBody College college ){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT tokenInfo = JWTUtil.getTokenInfo(token);
        String id = tokenInfo.getClaim("id").asString();
        college.setDeanId(Integer.valueOf(id));
        try {
            collegeService.addCollege(college);
            result.setStatus(1).setMsg("增加成功");
        }catch (Exception e){
            result.setStatus(0).setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("getCollegeById")
    public Result getCollegeById(@RequestParam int id){
        Result result = new Result();
        College college = collegeService.getCollegeById(id);
        result.setStatus(1).setMsg("获取成功").setData(college);
        return result;
    }
    @RequestMapping("updateCollege")
    public Result updateCollege(@RequestBody College college){
        Result result = new Result();
        try {
            collegeService.updateCollege(college);
            result.setStatus(1).setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(0).setMsg("修改失败");
        }
        return result;
    }
    @RequestMapping("delete")
    public Result delete(@RequestParam int id){
        Result result = new Result();
        collegeService.delete(id);
        try {
            result.setMsg("删除失败").setStatus(0);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("删除失败").setStatus(0);
        }
        return result;
    }
}
